package org.jeecg.modules.rooms.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.entity.BusHotel;
import org.jeecg.modules.rooms.DTO.CesMealCouponDto;
import org.jeecg.modules.rooms.DTO.CesMealCouponSearchDto;
import org.jeecg.modules.rooms.DTO.CesRoomSearchDto;
import org.jeecg.modules.rooms.DTO.CesRoomsDto;
import org.jeecg.modules.rooms.Vo.CesHousePriceSchemeVo;
import org.jeecg.modules.rooms.Vo.CesRoomsVo;
import org.jeecg.modules.rooms.Vo.SelectVo;
import org.jeecg.modules.rooms.entity.CesHousePriceScheme;
import org.jeecg.modules.rooms.entity.CesMealCoupon;
import org.jeecg.modules.rooms.entity.CesRoomBuildingFloor;
import org.jeecg.modules.rooms.entity.CesRooms;
import org.jeecg.modules.rooms.mapper.CesMealCouponMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 餐卷设置 服务实现类
 * </p>
 *
 * @author fendo
 * @since 2023-03-14
 */
@Service
public class CesMealCouponServiceImpl extends ServiceImpl<CesMealCouponMapper, CesMealCoupon> implements IService<CesMealCoupon> {
    @Resource
    private CesMealCouponMapper cesMealCouponMapper;

    /**
     * 获取当前酒店的所有餐券类型
     * @param hotelId
     * @return
     */
    public Result fetch(String hotelId){
        List<SelectVo> vos = new ArrayList<>();
        List<CesMealCoupon> list = cesMealCouponMapper.selectList(Wrappers.<CesMealCoupon>lambdaQuery().eq(CesMealCoupon::getHotelId,hotelId));
        list.forEach(v -> {
            SelectVo vo = new SelectVo();
            vo.setLabel(v.getName());
            vo.setValue(v.getId());
            vos.add(vo);
        });
        return Result.ok(vos);
    }

    /**
     * 查询餐券
     * @param dto
     * @return
     */
    public Result list(CesMealCouponSearchDto dto){

        QueryWrapper<CesMealCoupon> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isBlank(dto.getHotelId())){
            queryWrapper.eq(CesRooms.HOTEL_ID, dto.getHotelId());
        }
        queryWrapper.eq(CesRooms.INVALID,false);
        queryWrapper.orderByDesc(CesRooms.SORT, CesRooms.CREATEAT);

        IPage<CesMealCoupon> dataPage = cesMealCouponMapper.selectPage(new Page<>(dto.getPageNo().intValue(),dto.getPageSize().intValue()),queryWrapper);
        List<CesMealCoupon> records = dataPage.getRecords();
        List<CesMealCoupon> vos = new ArrayList<>();
        records.forEach(v->{
            CesMealCoupon vo = new CesMealCoupon();
            BeanUtil.copyProperties(v, vo);
            vos.add(vo);
        });

        Map<String,Object> map = new HashedMap();
        map.put("records",vos);
        map.put("total",dataPage.getTotal());
        return Result.OK(map);
    }

    /**
     * 创建餐券
     * @param cesMealCouponDto
     * @return
     */
    public Result create(CesMealCouponDto cesMealCouponDto){
        cesMealCouponDto.setCreateAt(LocalDateTime.now());
//        cesMealCouponDto.setSort(99);
//        cesMealCouponDto.setInvalid(false);
        cesMealCouponDto.setUpdateAt(LocalDateTime.now());
        cesMealCouponMapper.insert(cesMealCouponDto);
        return Result.ok("创建成功!");
    }

    /**
     * 编辑餐券
     * @param roomsDto
     * @return
     */
    public Result modify(CesRoomsDto roomsDto){
//        CesRooms room = baseMapper.selectOne(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getId, roomsDto.getId()).eq(CesRooms::getInvalid,false));
//        if(ObjectUtils.isEmpty(room)) return Result.ok("数据未找到!");
//        BeanUtil.copyProperties(roomsDto,room, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//        room.setUpdateAt(LocalDateTime.now());
//        roomsMapper.updateById(room);
        return Result.ok("修改成功!");
    }
}
